<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t("历史出资人") }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('基金简称')" prop="fundName">
        <el-input
          v-model="queryParams.fundName"
          :placeholder="$t('基金简称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('出资人名称')" prop="lpName">
        <el-input
          v-model="queryParams.lpName"
          :placeholder="$t('出资人名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="queryHandle"
        >{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("重置")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:manage:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:manage:edit']"
          type="success"
          icon="el-icon-upload"
          size="mini"
          @click="importHandle"
        >{{ $t("导入") }}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:manage:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button>
      </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="fundHistoryManageList"
      border
      tooltip-effect="darkTable"
    >
      <el-table-column
        :label="$t('序号')"
        type="index"
        align="center"
        show-overflow-tooltip
        width="50"
      >
        <template slot-scope="scope">
          {{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金简称')" align="center" prop="fundName" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.fundName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('基金规模') +$t('金额单位')"
        align="center"
        prop="fundScale"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.fundScale, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('成立时间')"
        align="center"
        prop="buildTime"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buildTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('出资人名称')" align="center" prop="lpName" />
      <el-table-column
        :label="$t('出资金额')+$t('金额单位')"
        align="center"
        prop="lpMoney"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.lpMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('出资占比(%)')"
        align="center"
        prop="lpPercent"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ scope.row.lpPercent }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('资金性质')"
        align="center"
        prop="fundQuality"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('介绍')"
        align="center"
        prop="lpRecommend"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('操作')"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:manage:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t("修改") }}</el-button>
          <el-button
            v-hasPermi="['system:manage:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t("删除") }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基金历史管理人对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <fundHistoryManageForm ref="fundHistoryManageForm" />
    </el-dialog>

    <!-- 导入窗口 -->
    <el-dialog
      :title="$t('导入')"
      :visible.sync="upload.open"
      width="400px"
      :close-on-click-modal="false"
      :lock-scroll="false"
    >
      <div>
        <el-upload
          ref="upload"
          v-loading="upload.isUploading"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :on-exceed="handleFileExceed"
          :on-change="handleFileChange"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">
            {{ $t("将文件拖到此处，或") }}
            <em>{{ $t("点击上传") }}</em>
          </div>
          <div slot="tip" class="el-upload__tip" style="color: red">
            {{ $t("提示：仅允许导入“xls”或“xlsx”格式文件！") }}
          </div>
        </el-upload>
        <el-button type="text" @click="downloadTemplet">{{
          $t("下载模板")
        }}</el-button>
        <div slot="footer" class="dialog-footer" style="text-align: right">
          <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="submitFileForm">{{
            $t("确定")
          }}</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      custom-class="menu-dialog-height"
    >
      <el-table
        :data="tableData"
        style="width: 100%"
        height="500"
        border
        :show-header="false"
      >
        <el-table-column prop="msg" label="错误信息" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listFundHistoryManage, delFundHistoryManage, exportFundHistoryManage }
from '@/api/fund/gp/history/fundHistoryManage'
import fundHistoryManageForm from './fundHistoryManageForm.vue'
import { getToken } from '@/utils/auth'
import { downLoadFileOpen } from '@/utils/zipdownload'
export default {
    components: {
        fundHistoryManageForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 基金历史管理人表格数据
          fundHistoryManageList: [],
          // 弹出层标题
          title: '历史出资人',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
             lpName: undefined,
              fundName: undefined,
              gpId: this.$route.query.gpId,
              pageNum: 1,
              pageSize: 10
          },
           // 项目导入参数
          upload: {
            // 是否显示弹出层（项目导入）
            open: false,
            // 弹出层标题（项目导入）
            title: '',
            // 是否禁用上传
            isUploading: false,
            // 设置上传的请求头部
            headers: { Authorization: 'Bearer ' + getToken() },
            // 上传的地址
            url: ''
          },
          // 表单参数
          form: { },
          tableData: [],
          dialogVisible: false,
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询基金历史管理人列表 */
    getList() {
        this.loading = true
        listFundHistoryManage(this.queryParams).then(response => {
            this.fundHistoryManageList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            fundName: undefined,
            fundScale: undefined,
            buildTime: undefined,
            lpName: undefined,
            lpMoney: undefined,
            gpId: undefined,
            lpPercent: undefined,
            fundQuality: undefined,
            lpRecommend: undefined,
            createTime: undefined,
            createBy: undefined,
            updateTime: undefined,
            updateBy: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.fundHistoryManageForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.fundHistoryManageForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delFundHistoryManage(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportFundHistoryManage(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    importHandle() {
       this.upload.open = true
       this.upload.url = process.env.VUE_APP_BASE_API + '/manage/fundHistoryManage/upload?gpId=' + this.$route.query.gpId
    },
    downloadTemplet() {
      //  downLoadFile()
      downLoadFileOpen('/manage/fundHistoryManage/download')
    },
     // 文件上传变更时间
    handleFileChange(file, fileList) {
      if (!(file.name.indexOf('xls') >= 0 && file.name.indexOf('xlsx') >= 0)) {
        this.msgError(this.$t('仅允许导入"xls"或"xlsx"格式文件!'))
        fileList.splice(0, 1)
      }
    },
     // 文件达到上限处理
    handleFileExceed() {
      this.msgError(this.$t('可上传文件数量已达到上限'))
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      if (response.msg === 'success') {
        this.msgSuccess(this.$t('导入成功'))
      } else if (response.code === 500) {
        this.msgError(response.msg)
      } else {
        this.dialogVisible = true
        this.tableData = response.data
      }

      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    }
  }
}
</script>
